package cn.wn.web;

import cn.wn.model.ResultInfo;
import cn.wn.pojo.User;
import cn.wn.service.PermissionService;
import cn.wn.service.UserService;
import cn.wn.shiro.ShiroRealm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("permission")
@Api(tags = "权限模块")
@Slf4j
public class PermissionController {

    @Autowired
    private ShiroRealm shiroRealm;
    @Autowired
    private PermissionService permissionService;

    /**
     * 角色权限（1）查询显示角色的权限；页面显示权限树
     */
    @GetMapping("/role/{roleId}")
    public ResultInfo findRolePermission(@PathVariable("roleId") Integer roleId) {
        return permissionService.findRolePermission(roleId);
    }

    /**
     * 角色权限（2）实现角色分配权限
     */
    @PutMapping("/role")
    public ResultInfo updateRolePermission(@RequestBody Map<String,Object> map) {
        String roleId = (String) map.get("roleId");
        ArrayList<Integer> permissions = (ArrayList<Integer>) map.get("permissions");
        // 调用service
        return permissionService.updateRolePermission(
                Integer.parseInt(roleId),
                permissions.toArray(new Integer[]{})
        );
    }
}
